package com.atguigu.eduservice.service;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.impl.EduVideoService;

/**
 * @author cyt
 * @create 2020-05-08 20:12
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {


    // 注入远程服务
    @Autowired
    VodClient vodClient;

    @Override
    public boolean removeVideoById(String id) {
        //查询云端视频id
        EduVideo eduVideo = baseMapper.selectById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        System.out.println("视频的id: " + videoSourceId);
        //删除视频资源
        if (!StringUtils.isEmpty(videoSourceId)) {
            R r = vodClient.removeVideo(videoSourceId);
            if (r.getCode() == 20001) {
                throw new GuliException(20001, "异常....  熔断器");
            }
        }

        Integer result = baseMapper.deleteById(id);

        return null != result && result > 0;
    }
}
